/**
 * 
 */
package com.efast.dmom.mes.datahandlers.entity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WorkOrderItem;
import com.efast.dmom.mes.dao.entity.LotDao;
import com.efast.dmom.mes.datahandlers.base.RuntimeDataHandler;
import com.efast.dmom.mes.entity.LotEntity;
import com.efast.dmom.mes.entity.WorkOrderItemEntity;
import com.efast.dmom.mes.entity.base.ITraceObject;

/**
 * @author Aaron Liu
 *
 *         2020年4月1日
 */
@Component
public class WorkOrderItemDataHandler extends RuntimeDataHandler<WorkOrderItem>{
	
	@Autowired
	private LotDao lotDao = null;
	
//	@Autowired
//	private 
	

	public void createLot(WorkOrderItem orderItem, int batch, BigDecimal batchSize) {

		if (orderItem == null || batchSize == null || batchSize.intValue() < 0) {
			return;
		}

		WorkOrderItemEntity itemEntity = orderItem.getInnerData();

		if (itemEntity.getOrderItemStatus() != ITraceObject.LOT_STATUS_CREATED) {
			return;
		}

		BigDecimal orderQuantity = itemEntity.getQuantityOrdered();
		if (orderQuantity == null || orderQuantity.intValue() < batch * batchSize.intValue()) {
			return;
		}
		
		List<LotEntity> entityList = new ArrayList<LotEntity>();

		for (int i = 0; i < batch; i++) {

			LotEntity lotEntity = new LotEntity();
			
			lotEntity.setBomName(itemEntity.getBomName());
			lotEntity.setBomRevision(itemEntity.getBomRevison());
			// lotEntity.setLotName(lotName);
			lotEntity.setLotType(ITraceObject.LOT_STATUS_CREATED);
			
			BigDecimal counter = new BigDecimal(i+1);
			BigDecimal left = orderQuantity.subtract(counter.multiply(batchSize));
			if(! (left.compareTo(BigDecimal.ZERO) == -1)){
				lotEntity.setQuantity(batchSize);
			}
			else {
				lotEntity.setQuantity(left);
			}
			
			lotEntity.setOrderKey(itemEntity.getOrderKey());
			lotEntity.setPartNumber(itemEntity.getPartNumber());
			lotEntity.setPartRevision(itemEntity.getPartRevision());
			lotEntity.setLotType(ITraceObject.LOT_TYPE_NORMAL);
			
			entityList.add(lotEntity);
		}
		lotDao.saveLotList(entityList);
	}


	@Override
	public ResultBean save(WorkOrderItem obj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ResultBean update(WorkOrderItem obj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ResultBean delete(WorkOrderItem obj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ResultBean saveBatch(List<WorkOrderItem> obj) {
		
		ResultBean resultBean = new ResultBean(true);
		
		
		
		
		return resultBean;
	}


	@Override
	public ResultBean updateBatch(List<WorkOrderItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ResultBean deleteBatch(List<WorkOrderItem> obj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public WorkOrderItem getById(long id) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
